package rerac.components;

import goalie.Component;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import rerac.feature.DocumentParser;
import rerac.protos.Corpus.Document;

/**
 * This parses single sentence documents using the stanford nlp tagger.
 * 
 * @author Benjamin Roth
 *
 */
public class ParseSingleSentenceDocuments implements Component {
  
  public static final String REFERRED_NAME = "parse_sentences";

  @Override
  public void cleanup(Map<String, String> outputs) throws IOException {
    File f = new File(outputs.get("output"));
    f.delete();
  }

  @Override
  public Map<String, String> run(Map<String, String> params) throws IOException {
    String inputFN = params.get("input");
    String outputFN = params.containsKey("output_destination") ?
        params.get("output_destination") : inputFN + ".parsed" ;
    int maxEntries = Integer.MAX_VALUE;
    if (params.containsKey("max_entries")) {
      maxEntries = Integer.parseInt(params.get("max_entries"));
    }
    String grammarFN = params.get("grammar");
        
    BufferedInputStream is = new BufferedInputStream(new FileInputStream(
        inputFN));
    DataOutputStream output = new DataOutputStream(new BufferedOutputStream(
        new FileOutputStream(outputFN)));
    
    DocumentParser parser = new DocumentParser(grammarFN);
    int numEntries = 0;
    for (Document doc; 
         numEntries < maxEntries && 
         (doc = Document.parseDelimitedFrom(is)) != null;) {
      Document parsedDoc = parser.parse(doc);
      parsedDoc.writeDelimitedTo(output);
      ++numEntries;
    }
    output.close();
    is.close();
    Map<String, String> outMap = new HashMap<String, String>();
    outMap.put("output", outputFN);
    return outMap;
  }
}
